package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.training.domain.TrainingAchievement;
import com.weishi.project.training.domain.TrainingGroup;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.mapper.TrainingGroupMapper;
import com.weishi.project.training.service.ITrainingAchievementService;
import com.weishi.project.training.service.ITrainingGroupChannelService;
import com.weishi.project.training.service.ITrainingGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 训练小组Service业务层处理
 */
@Service
public class TrainingGroupServiceImpl extends ServiceImpl<TrainingGroupMapper, TrainingGroup> implements ITrainingGroupService {

    @Autowired
    private TrainingGroupMapper trainingGroupMapper;

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;


    /**
     * 查询训练小组列表
     */
    @Override
    public List<TrainingGroup> selectTrainingGroupList(TrainingGroup trainingGroup) {
        return trainingGroupMapper.selectTrainingGroupList(trainingGroup);
    }

    @Override
    public List<TrainingGroup> selectTrainingGroupList1(TrainingGroup trainingGroup) {
        return trainingGroupMapper.selectTrainingGroupList1(trainingGroup);
    }

    @Override
    public List<TrainingGroup> getGroupNameList() {
        return trainingGroupMapper.getGroupNameList();
    }

    @Override
    public List<TrainingGroup> getTrainingGroup(Map<String, Object> map) {
        return trainingGroupMapper.getTrainingGroup(map);
    }

    @Override
    public Integer getTrainingDayIdByGroupId(int groupId) {
        return trainingGroupMapper.getTrainingDayIdByGroupId(groupId);
    }

    @Override
    public boolean removeByIds(List<Integer> idList) {
        boolean ret = false;
        for(Integer trainingGroupId : idList){
            ret = removeById(trainingGroupId);
        }
        return ret;
    }

    /**
     * 删除训练组同步删除训练航道的相关数据
     * @param trainingGroupId
     * @return
     */
    @Transactional
    @Override
    public boolean removeById(Integer trainingGroupId){
        List<TrainingGroupChannel> trainingGroupChannels = trainingGroupChannelService.getTrainingGroupChannelByGroupId(trainingGroupId,false);
        List<Integer> channelIds = new ArrayList<>();
        for(TrainingGroupChannel trainingGroupChannel : trainingGroupChannels){
            channelIds.add(trainingGroupChannel.getId());
        }
        trainingGroupChannelService.removeByIds(channelIds);
        int ret = trainingGroupMapper.deleteById(trainingGroupId);
        return ret==0?false:true;
    }

    @Override
    public List<TrainingGroup> selectTrainingGroupListByTrainingDayId(Integer trainingDayId) {
        TrainingGroup trainingGroup = new TrainingGroup();
        trainingGroup.setTrainingDayId(trainingDayId);
        return trainingGroupMapper.selectTrainingGroupList1(trainingGroup);
    }

    @Override
    public int copyGoUpGroup(Integer sourceId, Integer destId) {
        Map<String, Object> params = new HashMap<>();
        params.put("groupId", sourceId);
        List<TrainingAchievement> sourceAchievements = trainingAchievementService.trainingAchievementList(params);
        params.put("groupId", destId);
        List<TrainingAchievement> destAchievements = trainingAchievementService.trainingAchievementList(params);
        Set<Integer> destSportsman = new HashSet<>();
        for(TrainingAchievement achievement : destAchievements){
            destSportsman.add(achievement.getSportsmanId());
        }

        for(TrainingAchievement achievement : sourceAchievements){
            if(destSportsman.contains(achievement.getSportsmanId())){
                continue;
            }
            TrainingGroupChannel channeldb = trainingGroupChannelService.getById(achievement.getChannelId());

            TrainingGroupChannel channel = new TrainingGroupChannel();
            channel.setGroupId(destId);
            channel.setTrainingMode(channeldb.getTrainingMode());
            channel.setRowingKilometers(channeldb.getRowingKilometers());
            channel.setRowingClassHour(channeldb.getRowingClassHour());
            achievement.setId(null);
            achievement.setChannelId(null);
            List<TrainingAchievement> person = new ArrayList<>();
            TrainingAchievement temp = new TrainingAchievement();
            temp.setSportsmanId(achievement.getSportsmanId());
            temp.setSportsmanName(achievement.getSportsmanName());
            temp.setGroupId(destId);
            temp.setWeekId(achievement.getWeekId());
            temp.setTrainingDayId(achievement.getTrainingDayId());
            temp.setPosition(achievement.getPosition());
            person.add(temp);
            channel.setPerson(person);
            trainingGroupChannelService.add(channel);
            destSportsman.add(achievement.getSportsmanId());
        }


        return 1;
    }
}